﻿(function ($) {
    // ----------------------------------------------------------------------- 页面初始化 -----------------------------------------------------------------
    function init() {
        $(document.body).on("contextmenu", function (e) {
            var target = e.srcElement;

            if (target == null || e.srcElement.tagName !== 'INPUT') {
                return false;
            }

            var t = target.type;
            return (t === "text" || t === "password" || t === "textarea");
        });
    }

    // ------------------------------------------------------------ prototype扩展 ---------------------------------------------------------------------
    Array.prototype.findItem = function (attrName, eqValue) {
        if (attrName == null) return null;
        if (typeof Array.prototype.find === 'function') {
            return this.find(function (item) {
                return item[attrName] === eqValue;
            });
        } else {
            var findedList = this.filter(function (item) {
                return item[attrName] === eqValue;
            });
            if (findedList) return findedList[0];
            else return null;
        }
    };

    // ------------------------------------------------------------ 以下为UI操作封装 ---------------------------------------------------------------------
    // alert
    function alert(msg) {
        vm.popShow('suc', 'alert', msg);
    }
    // err
    function error(msg) {
        vm.popShow('err', 'alert', msg);
    }
    // confirm
    function confirm(msg, suc, err) {
        vm.popShow('ask', 'confirm', msg,suc,err);
    }
    // blockUI
    function blockUI(msg) {
        msg = typeof (msg) == 'undefined' ? '处理中......' : msg;
        $('#loading-text').html(msg);
        // 防止重复弹出等待窗
        if ($('.loading-fixed').hasClass('visible')) {
            return;
        }

        $(".loading-fixed").addClass('visible');
    }
    // unblockUI
    function unblockUI() {
        $(".loading-fixed").removeClass('visible');
    }

    
    // --------------------------------------------------- 以下为帮助方法封装 -----------------------------------------------------------------------------
    function post(url, ajaxData, suc) {
        // 参数初始化
        var options = Object.prototype.toString.call(url) === "[object Object]" ? url : {
            url: url,
            ajaxData: ajaxData,
            suc: suc,
        };
        options.block = options.block == null ? true : options.block;
        var url = options.url;
        var ajaxData = options.ajaxData;
        var el = options.el;
        var suc = options.suc;
        var sucMsg = options.sucMsg;
        var err = options.err;
        var block = options.block;
        // ajax 请求
        var me = this;
        if(el){
            $(el).prop('disabled','disabled');
        }
        if (block) {
            me.blockUI();
        }
        $.post(url, ajaxData, function (data) {
            if (data.result == "Success") {
                if(el){
                    $(el).removeProp('disabled');
                }
                if (suc) {
                    suc(data.data);
                }
                if(sucMsg){
                    me.success(sucMsg);
                }
                if (block) {
                    me.unblockUI();
                }
            } else {
                if(el){
                    $(el).removeProp('disabled');
                }
                me.error(data.desc);
                console.log(data.error);
                if($.isFunction(err)){
                    err(data);
                }
                if (block) {
                    me.unblockUI();
                }
            }
        }, 'json').fail(function(e){
            me.error('请求失败'+'( '+e.statusText+' )');
            console.log(e.responseText);
            if(el){
                $(el).removeProp('disabled');
            }
            if($.isFunction(err)){
                err(e);
            }
            if (block) {
                me.unblockUI();
            }
        })
    }

    //格式化时间为两位
    function timeFommat(obj) {
        if (obj < 10)
            return "0" + "" + obj;
        else
            return obj;
    }
    // ------------------------------------------------------ 初始化、全局注册 --------------------------------------------------------------------------------------------
    init();
    window.helper = window.$$ = {
        alert: alert,
        error: error,
        confirm: confirm,
        blockUI: blockUI,
        unblockUI: unblockUI,
        timeFommat: timeFommat,
        post: post,
    };
})($);  